<?php
require_once 'WKernel/Markup/Parser.php';
require_once 'WKernel/Markup/Document.php';

require_once 'WKernel/Data/Map.php';
require_once 'WKernel/Data/Collection.php';
require_once 'WKernel/Data/Loader/Xml.php';
require_once 'WKernel/Data/Serializer/Xml.php';
require_once 'WKernel/Data/Loader/Json.php';
require_once 'WKernel/Data/Serializer/Json.php';

/**
 * Common data container, utility class
 * 
 * @package WKernel
 * 
 */
class WK_Data {
	private static $LOADERS     = array('XML'  => 'WK_Data_Loader_Xml', 
										'JSON' => 'WK_Data_Loader_Json');
	private static $SERIALIZERS = array('XML'  => 'WK_Data_Serializer_Xml', 
										'JSON' => 'WK_Data_Serializer_Json');
	/**
	 * Load data representation objects from XML file
	 * @param string $file
	 */
	public static function load($string_data, $format = 'XML'){
		$loader = self::$LOADERS[$format];
		if(class_exists($loader)){
			$loader      = new $loader ();
			$data_object = $loader->load($string_data);
		}
		else
			throw new Exception(
				'Data loader for "' . $format . '" does not exist (' . $loader . ')', 
				WERR_UNKNOWN_DATA_LOADER);
				
		return $data_object;
	}
	
	public static function serialize($data_object, $format){
		$serializer = self::$SERIALIZERS[$format];
		if(class_exists($serializer)){
			$serializer = new $serializer ($data_object);
		}
		else
			throw new Exception(
				'Data serializer for "' . $format . '" does not exist (' . $serializer . ')', 
				WERR_UNKNOWN_DATA_SERIALIZER);
		return $serializer->serialize();
	}
}

